import 'package:banquanjie_app/app/services/screenAdapter.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
// import 'package:banquanjie_app/app/widget/common/dropdownMenu.dart';
import '../controllers/work_list_controller.dart';
import 'package:banquanjie_app/app/widget/common/customScaffold.dart';
import 'package:dropdown_button2/dropdown_button2.dart';
import 'package:banquanjie_app/app/widget/work/videoList.dart';
import 'package:banquanjie_app/app/widget/work/scriptList.dart';
import 'package:banquanjie_app/app/widget/work/letterList.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
class WorkListView extends GetView<WorkListController> {
  const WorkListView({super.key});
  static Widget _buildSortContent(String title) {
    return Column(
      children: [
        _buildFilterItem(title, true),
        _buildFilterItem('销量优先', false),
        _buildFilterItem('好评优先', false),
      ],
    );
  }


  static Widget _buildFilterItem(String text, bool isSelected) {
    return Container(
      height: 44,
      color: isSelected ? Colors.orange[50] : Colors.white,
      child: Center(
        child: Text(
          text,
          style: TextStyle(
            color: isSelected ? Colors.orange : Colors.black,
          ),
        ),
      ),
    );
  }
  

  @override
  Widget build(BuildContext context) {
    
    return Customscaffold(
      title: '经典IP',
      child: Obx(() =>Column(
        children: [
          Container(
            width: double.infinity,
            height: ScreenAdapter.width(88),
            decoration: BoxDecoration(
              color: Colors.white,
              border: Border(top: BorderSide(color: Color(0XFFF4F7FC)))
            ),
            child: Row(
              children: [
                Expanded(
                  flex: 1,
                  child: Row(
                    children: [SizedBox(width: ScreenAdapter.width(28)),
                      Container(
                        margin: EdgeInsets.only(right: ScreenAdapter.height(22)),
                        width: ScreenAdapter.width(108),
                        height: ScreenAdapter.width(50),
                        child: ElevatedButton(
                          style: ButtonStyle(
                            padding: WidgetStatePropertyAll(EdgeInsets.all(0)),
                            backgroundColor: WidgetStateProperty.all(controller.query['sortType'] == 1 ? Color(0xFF00B277) : Color(0xFFF2F2F2)),
                            foregroundColor: WidgetStateProperty.all(controller.query['sortType'] == 1 ? Colors.white : Colors.black),
                          ),
                          onPressed: (){
                            controller.setSearchQueryValue("sortType", 1);
                          },
                          child: Text('最新', style: TextStyle(fontSize: ScreenAdapter.fontSize(28)),)
                        )
                      ),
                      Container(
                        margin: EdgeInsets.only(right: ScreenAdapter.height(22)),
                        width: ScreenAdapter.width(108),
                        height: ScreenAdapter.width(50),
                        child: ElevatedButton(
                          style: ButtonStyle(
                            padding: WidgetStatePropertyAll(EdgeInsets.all(0)),
                            backgroundColor: WidgetStateProperty.all(controller.query['sortType'] == 2 ? Color(0xFF00B277) : Color(0xFFF2F2F2)),
                            foregroundColor: WidgetStateProperty.all(controller.query['sortType'] == 2 ? Colors.white : Colors.black),
                          ),
                          onPressed: (){
                            controller.setSearchQueryValue("sortType", 2);
                          },
                          child: Text('推荐', style: TextStyle(fontSize: ScreenAdapter.fontSize(28)),)
                        )
                      ),
                    ],
                  )
                ),
                DropdownButtonHideUnderline(
                  child: DropdownButton2<String>(
                    isExpanded: true,
                    // hint: Text(
                    //   '',
                    //   style: TextStyle(
                    //     fontSize: ScreenAdapter.fontSize(28),
                    //     color: Theme.of(context).hintColor,
                    //   ),
                    // ),
                    items: controller.worksTypeFirstList
                      .map((item) => DropdownMenuItem<String>(
                          value: "${item['id']}",
                          child: Text(
                            "${item['dicName']}",
                            style: TextStyle(
                              fontSize: ScreenAdapter.fontSize(28),
                            ),
                          ),
                        )
                      ).toList(),
                    value: controller.query['worksTypeFirst'],
                    onChanged: (String? value) {
                      // setState(() {
                      //   selectedValue = value;
                      // });
                      controller.setSearchQueryValue('worksTypeFirst', value);
                      print(value);
                    },
                    dropdownStyleData: DropdownStyleData(
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(2),
                        color: Colors.white,
                      ),
                      offset: const Offset(-20, 0),
                    ),
                    // buttonStyleData: const ButtonStyleData(
                    //   padding: EdgeInsets.symmetric(horizontal: 16),
                    //   height: 40,
                    //   width: 140,
                    // ),
                    customButton: Container(
                      width: ScreenAdapter.width(250),
                      height: ScreenAdapter.width(80),
                      padding: EdgeInsets.symmetric(horizontal: ScreenAdapter.width(16)),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Expanded(
                            child: Text(
                              controller.query['worksTypeFirst'].isNotEmpty ? 
                              controller.worksTypeFirstList
                                  .firstWhere(
                                    (item) => "${item['id']}" == controller.query['worksTypeFirst'],
                                    orElse: () => null,
                                  )['dicName'] : '作品类型',
                              textAlign: TextAlign.right, // 文字靠右
                              style: TextStyle(fontSize: ScreenAdapter.fontSize(28)),
                              overflow: TextOverflow.ellipsis,
                            ),
                          ),
                          const Icon(Icons.arrow_drop_down),
                        ],
                      ),
                    ),
                    menuItemStyleData: MenuItemStyleData(
                      height: ScreenAdapter.width(70),
                    ),
                  ),
                ),
                
                // SizedBox(
                //   width: ScreenAdapter.width(160),
                //   child: DropDownMenu(
                //     items: const ['综合'],
                //     dropItems: [
                //       _buildSortContent('综合排序'),
                //     ],
                //     selectedColor: Colors.orange,
                //   )
                // )
                
              ]
            ),
          ),
          
          Expanded(
            flex: 1,
            child: controller.total.value == 0 ? Center(child: Text('暂无数据')) : RefreshIndicator(
              onRefresh: controller.onRefresh,
              child: MasonryGridView.extent(
                  controller: controller.scrollController,
                  maxCrossAxisExtent: ScreenAdapter.width(348),
                  padding: EdgeInsets.all(ScreenAdapter.width(20)),
                  mainAxisSpacing: ScreenAdapter.width(20),
                  crossAxisSpacing: ScreenAdapter.width(20),
                  itemCount: controller.workList.length,
                  itemBuilder: (context, index) {
                    if (controller.workList[index]['worksTypeFirst'] == 204) 
                      // ignore: curly_braces_in_flow_control_structures
                      return Scriptlist(item: controller.workList[index]);
                    else if (controller.workList[index]['worksTypeFirst'] == 205)
                      // ignore: curly_braces_in_flow_control_structures
                      return LetterList(item: controller.workList[index]);
                    else 
                      // ignore: curly_braces_in_flow_control_structures
                      return VideoList(item: controller.workList[index]);
                  }
                )
              )
            )
        ])
      )
    );
  }
}
